﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KamLib
{

    public class UilContext : IDisposable
    {
        public readonly Dictionary<string, UilController> Controllers = new Dictionary<string, UilController>();

        public readonly HashSet<string> ReloadedStores = new HashSet<string>();
        public LogWriter Log;

        public UilController GetController(string lid)
        {
            var node = Controllers.Try(lid);
            if (node == null)
                Controllers[lid] = node = UilController.NewController(this, lid);
            return node;
        }
        public T GetNode<T>() where T : UilController, new()
        {
            var node = Controllers.Values.OfType<T>().FirstOrDefault();
            if (node == null)
            {
                var nodeType = UilController.ControllerTypes.Where(a => a.Value == typeof(T)).First();
                Controllers[nodeType.Key] = node = (T)new T().Initialize(this);
            }
            return node;
        }

        public UilNode GetLogic(UiKey key)
        {
            if (key == null) return null;
            var node = GetController(key.LID);
            if (node == null) return null;
            return (UilNode)node.GetForm(key) ?? node.GetList(key);
        }

        public FormLogic GetForm(UiKey key)
        {
            if (key == null) return null;
            var node = GetController(key.LID);
            if (node == null) return null;
            return node.GetForm(key);
        }
        public ListLogic GetList(UiKey key)
        {
            if (key == null) return null;
            var node = GetController(key.LID);
            if (node == null) return null;
            return node.GetList(key);
        }

        public EntityDescAttribute GetEntityDesc(UiKey key)
        {
            var logic = GetLogic(key);
            return logic == null ? null : logic.EntityDesc;
        }

        public void Dispose()
        {
            foreach (var node in Controllers.Values)
            {
                node.Dispose();
            }
        }


        #region Notifications

        public UilNotification.List Notifications = new UilNotification.List();

        #endregion
    }


    public class UiFormLogicNodeInfo : IDisposable
    {
        public UilController Node;
        public string LIID;
        public Type Type;
        public string[] KeyFields;
        public Dictionary<UiKey, FormLogic> Items;
        public FormLogic GetForm(UiKey key)
        {
            return Node.GetForm(this, key);
        }


        public void Dispose()
        {
            if (Items == null) return;
            foreach (var item in Items.Values)
            {
                item.Dispose();
            }
        }
    }
    public class UiListLogicNodeInfo
    {
        public UilController Node;
        public string LIID;
        public Type Type;
        public Dictionary<string, ListLogic> Items;
        public ListLogic GetList(string id = "0")
        {
            return Node.GetList(this, id);
        }

        public void Dispose()
        {
            if (Items == null) return;
            foreach (var item in Items.Values)
            {
                item.Dispose();
            }
        }

    }

}
